﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXZPrint.Models.Entity;

namespace XXZPrint.Models.DataOperation
{
    public class C_Do_HelpDoc
    {
        /// <summary>
        /// 获取所有文档信息列表
        /// </summary>
        /// <returns></returns>
        public List<Docs> GetDocsList()
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<Docs>().ToList();
            }
        }

        /// <summary>
        /// 获取文档信息
        /// </summary>
        /// <param name="id">文档ID号</param>
        /// <returns></returns>
        public Docs GetDocs(int id)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<Docs>().Where(w => w.d_id == id).First();
            }
        }

        /// <summary>
        /// 获取文档信息
        /// </summary>
        /// <param name="name">文档名称</param>
        /// <returns></returns>
        public Docs GetDocs(string name)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<Docs>().Where(w => w.d_name == name).First();
            }
        }

        public int DocEdit(Docs data)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                if (data.d_id == 0)
                {
                    data = iDbContext.DbContext.Insert<Docs>(data);
                    return data == null ? 0 : 1;
                }
                else
                {
                    return iDbContext.DbContext.Update<Docs>(data);
                }
            }
        }

        /// <summary>
        /// 删除指定的文档数据
        /// </summary>
        /// <param name="id">文档ID</param>
        /// <returns></returns>
        public int DocDel(int id)
        {
            using(C_DbContext iDbContext=new C_DbContext())
            {
                iDbContext.DbContext.Delete<MenuDocs>(w => w.d_id == id);//同步删除挂载在菜单下的文档记录
                return iDbContext.DbContext.DeleteByKey<Docs>(id);
            }
        }

        /// <summary>
        /// 获取未在指定菜单下的文档数据
        /// <para>用于编辑菜单下的挂载帮助文档</para>
        /// </summary>
        /// <param name="menuid">菜单id</param>
        /// <returns></returns>
        public List<Docs> GetNotInMenuDocs(int menuid)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<Docs>()
                    .Where(
                    w =>
                    !iDbContext.DbContext.Query<MenuDocs>()
                    .Where(v => v.m_id == menuid)
                    .Select(did => did.d_id)
                    .ToList()
                    .Contains((int)w.d_id)
                    ).ToList();
            }
        }

        /// <summary>
        /// 获取指定菜单下的文档数据
        /// </summary>
        /// <param name="menuid">菜单ID</param>
        /// <returns></returns>
        public List<Docs> GetInMenuDocs(int menuid)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<Docs>()
                    .Where(
                    w =>
                    iDbContext.DbContext.Query<MenuDocs>()
                    .Where(v => v.m_id == menuid)
                    .Select(did => did.d_id)
                    .ToList()
                    .Contains((int)w.d_id)
                    ).ToList();
            }
        }

        /// <summary>
        /// 获取指定名称的帮助文档的内容
        /// </summary>
        /// <param name="name">唯一性名称</param>
        /// <returns></returns>
        public string GetHtmlContent(string name)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                Docs iData = iDbContext.DbContext.Query<Docs>()
                    .Where(w => w.d_name == name)
                    .FirstOrDefault();

                return iData.d_content;
            }
        }
    }
}